﻿using MassTransit;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Tiens.NotificationHub.IntegrationEvents;
using Tiens.NotificationHub.Services;

namespace Tiens.NotificationHub.Core.Consumers
{
    public class SendTextEmailConsumer : IConsumer<SendTextMessageEmailEvent>
    {
        private readonly ISmtpMailService _stmpMailService;
        private readonly ILogger<SendTextEmailConsumer> logger;

        public SendTextEmailConsumer(ISmtpMailService stmpMailService,ILogger<SendTextEmailConsumer> logger)
        {
            _stmpMailService = stmpMailService;
            this.logger = logger;
        }

        public async Task Consume(ConsumeContext<SendTextMessageEmailEvent> context)
        {
            var content = context.Message;

            logger.LogInformation("SendTextEmailConsumer,Content[id:{id},{message}] start...", content.Id, JsonConvert.SerializeObject(content));
            await _stmpMailService.SendTextMessageAsync(content.FromAddress, content.ToAddress,
                content.Subject, content.Body);
            logger.LogInformation("SendTextEmailConsumer,id:{id} end.", content.Id);

        }
    }
}
